package com.hydl.power.controller;

import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.Navbar;
import com.hydl.power.pojo.NavbarVO;
import com.hydl.power.pojo.PowerRoom;
import com.hydl.power.pojo.SysMenuVO;
import com.hydl.power.service.NavBarService;
import com.hydl.power.service.PowerRoomService;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author wxb
 * @date 2020/3/21 16:31
 */
@RestController
@RequestMapping("/navbar")
@Api(value = "NavbarController", tags = "导航栏")
@CrossOrigin
public class NavbarController {

    @Autowired
    private NavBarService navBarService;
    @Autowired
    private PowerRoomService powerRoomService;


    @LogAnnotation(module = "导航栏",description = "新增单位项目/行业")
    @PostMapping("addNavbar")
    @ApiOperation(value = "新增单位项目/行业,如果没有行业直接添加", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "navName", value = "行业名字", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer"),
    })
    public Resp addNavbar(String navName,Integer id) {
        int i = navBarService.addNavbar(navName,id);
        if (i == 1){
            return Resp.success("Success");
        }
        return Resp.success("添加失败");
    }


    //  @LogAnnotation(module = "导航栏",description = "地区/单位项目")
    @GetMapping("getNavbar")
    @ApiOperation(value = "导航栏,地区/单位项目", produces = "application/json")
    public Resp<LinkedHashSet<NavbarVO>> getNavbar() {
        Set<NavbarVO> set = navBarService.getNavbar();
        return Resp.success(set);
    }



    @LogAnnotation(module = "导航栏",description = "根据ID删除项目/地区")
    @ApiOperation(value="根据ID删除项目/地区")
    @PostMapping("/deleteNavbarById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "navbarId", value = "navbarId",  required = true, dataType = "String")
    })
    public Resp deleteNavbar(String navbarId){
        List<PowerRoom> powerRooms = powerRoomService.selectNavbarByNavId(navbarId);
        if (powerRooms.size() != 0){
            return Resp.fail("子菜单有数据,不能删除");
        }
        int i =  navBarService.deleteById(navbarId);
        if (i == 1){
            return Resp.success("删除成功");
        }else{
            return Resp.fail("删除失败");
        }
    }
}
